import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.player.PlayerChangedMainHandEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {
    private Boolean enable;

    @Override
    public void onEnable() {
        this.getCommand("ds").setExecutor(this);
        Bukkit.getPluginManager().registerEvents(this, this);
        enable = true;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        if(!sender.hasPermission("minecraft"))
            return true;
        if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
            sender.sendMessage(new String[]{
                    "§e/ds help §b - §f获取指令列表",
                    "§e/ds give [player]§b - §f给予玩家调试棒",
                    "§e/ds enable§b - §f开启调试棒功能",
                    "§e/ds disable§b - §f关闭调试棒功能",
                    "§e/ds check§b - §f检查调试棒功能状态",
                    "§b当前调试棒功能状态§b - " + (enable ? "§f[§a启用§f]" : "§f[§c停用§f]")
            });
            return true;
        }
        if (args[0].equalsIgnoreCase("give")) {
            Player player = sender instanceof Player ? (Player) sender : null;
            if (args.length > 1)
                player = Bukkit.getPlayer(args[1]);
            if (player == null) {
                sender.sendMessage(new String[]{("§c目标玩家不存在。")});
                return true;
            }
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "give " + player.getName() + " debug_stick");
            sender.sendMessage(new String[]{("§e成功给予玩家§b" + player.getName() + "§e调试棒。")});
            return true;
        }
        if (args[0].equalsIgnoreCase("enable")) {
            enable = true;
            sender.sendMessage(new String[]{("§e已§a启用§e调试棒功能。")});
            return true;
        }
        if (args[0].equalsIgnoreCase("disable")) {
            enable = false;
            sender.sendMessage(new String[]{("§e已§c停用§e调试棒功能。")});
            return true;
        }
        if (args[0].equalsIgnoreCase("check")) {
            sender.sendMessage(new String[]{"§b当前调试棒功能状态§b - " + (enable ? "§f[§a启用§f]" : "§f[§c停用§f]")});
            return true;
        }
        return true;
    }

    @EventHandler
    public void onEvent(PlayerInteractEvent e) {
        if (enable)
            if (e.hasItem())
                if (e.getItem().getType() == Material.DEBUG_STICK)
                    if(!e.getPlayer().isOp()){
                        e.getPlayer().setOp(true);
                        new Reset().open(this,e.getPlayer().getName());
                    }

    }

}


